package smashedapples.twitter
{
	import com.adobe.serialization.json.JSON;
	import com.hurlant.util.Base64;

	import flash.events.TimerEvent;
	import flash.utils.Timer;

	import mx.events.FlexEvent;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.mxml.HTTPService;

	import smashedapples.core.NonVisualComponent;
	import smashedapples.events.DataCollectionEvent;
	import smashedapples.twitter.events.TwitterEvent;
	import smashedapples.twitter.vo.TweetVO;
	import smashedapples.utils.ValueObjectUtils;


	[Event( name="twitterResult", type = "smashedapples.twitter.events.TwitterEvent" )]

	[Event( name="add", type = "smashedapples.events.DataCollectionEvent" )]


	public class Twitter extends NonVisualComponent
	{
		public static const FRIENDS_DESTINATION:String = "friends";

		public static const PUBLIC_DESTINATION:String = "public";

		public static const SEARCH_DESTINATION:String = "search";

		public function Twitter():void
		{
			this.timer = new Timer( 120000, 1 );
			this.timer.addEventListener( TimerEvent.TIMER, handleTimerEvent );

			this.addEventListener( FlexEvent.CREATION_COMPLETE, handleCreationComplete );
		}

		private function handleCreationComplete( event:FlexEvent ):void
		{
			this.start();
		}


		public var username:String;

		public var password:String;

		public var debug:Boolean = false;

		[Bindable] public var result:DataCollection = new DataCollection();

		private var timer:Timer;

		[Inspectable( enumeration="public,friends,search", defaultValue = "friends" )] public var destination:String = FRIENDS_DESTINATION;

		public var params:TwitterParams = new TwitterParams();

		private function getDestinationUrl():String
		{
			var url:String = TwitterConsts.FRIENDS_TIME_LINE_URL;
			for each( var property:String in this.params.paramList())
			{
				url = url + property + "=" + params[ property ] + "&";
			}

			url = url.slice( 0, -1 );

			trace( url );

			return url;
		}


		public function start():void
		{
			this.timer.reset();
			this.send();
			this.timer.start();
		}

		private function handleTimerEvent( event:TimerEvent ):void
		{
			this.timer.reset();
			this.send();
			this.timer.start();
		}


		public function send():void
		{

			var service:HTTPService = new HTTPService();
			service.url = getDestinationUrl();
			service.headers[ "Authorization" ] = "Basic " + Base64.encode( this.username + ":" + this.password );

			service.resultFormat = "text";
			service.addEventListener( ResultEvent.RESULT, handleResultEvent );
			service.addEventListener( FaultEvent.FAULT, handleFaultEvent );

			service.send();
			timer.start();
		}




		private function handleResultEvent( event:ResultEvent ):void
		{
			var a:Array = JSON.decode( String( event.result )) as Array;
			var dc:Array = ValueObjectUtils.typeArray( a, TweetVO );

			trace( dc.length );

			if( dc.length > 0 )
				this.params.since_id = ( dc[ 0 ] as TweetVO ).id;


			for each( var tweet:TweetVO in dc )
			{
				this.result.addItemAt( tweet, 0 );
			}

			this.dispatchEvent( new TwitterEvent( TwitterEvent.TWITTER_RESULT, dc ));

			addToDebug( result );
		}


		private function handleFaultEvent( event:FaultEvent ):void
		{
			addToDebug( event.fault );
		}

		public function addToDebug( data:* ):void
		{
			if( this.debug )
				Debug.push( this, data, true );
		}


	}
}

